package com.group.three.controller.member;

import com.group.three.entity.MemberAddDTO;
import com.group.three.entity.MemberVO;
import com.group.three.service.AfterSaleService;
import com.group.three.service.MemberService;
import com.group.three.untils.SnowFlakeUtil;
import com.group.three.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 管理端,会员
 */
@RestController
@Api(tags = "管理端,会员列表")
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "会员总列表")
    @GetMapping("/getAll")
    public JsonResult getByPage(int pageNum, int pageSize) {
        List<MemberVO> memberList = memberService.getAll(pageNum, pageSize);
        if (memberList != null) {
            return new JsonResult("200", "会员用户列表查询成功", memberList);
        }
        return new JsonResult("400", "会员用户列表查询失败");
    }

    @ApiOperation(value = "通过ID获取会员信息")
    @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public JsonResult get(@PathVariable String id) {
        MemberVO member = memberService.getMember(id);
        if (member != null) {
            return new JsonResult("200", "会员用户查询成功", member);
        }
        return new JsonResult("400", "会员用户查询失败");

    }


    @ApiOperation(value = "添加会员")
    @PostMapping("/addmember")
    public JsonResult save(MemberAddDTO member) {
        String s = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
        String substring = s.substring(0, s.length() - 3);
        member.setId(substring);
        int i = memberService.addMember(member);
        if (i == 1) {
            return new JsonResult("200", "会员增加成功");
        }
        return new JsonResult("400", "会员增加失败");
    }


    @ApiOperation(value = "修改会员状态,开启关闭会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "String", allowMultiple = true, paramType = "query"),
            @ApiImplicitParam(name = "disabled", value = "TRUE:开启，FALSE:关闭",required = true, dataType = "String", paramType = "query")
    })
    @PutMapping("/updateMemberStatus")
    public JsonResult updateMemberStatus(@RequestParam String memberId, @RequestParam String disabled) {
        int i = memberService.updateMemberStatus(memberId, disabled);
        if (i == 1) {
            return new JsonResult("200", "会员状态修改成功");
        }
        return new JsonResult("400", "员状态修改失败");
    }
}
